package com.mall.mallproduct.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.mall.mallproduct.service.CategoryBrandRelationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.utils.PageUtils;
import com.common.utils.Query;

import com.mall.mallproduct.dao.CategoryDao;
import com.mall.mallproduct.entity.CategoryEntity;
import com.mall.mallproduct.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Autowired
    CategoryBrandRelationService categoryBrandRelationService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {

        List<CategoryEntity> entities = baseMapper.selectList(null);

        //一级目录分类
        List<CategoryEntity> level1Menus=new ArrayList();

        for (CategoryEntity entity : entities) {
            if (entity.getParentCid()==0){

                entity.setChildren(getChildren(entity,entities));

                level1Menus.add(entity);
            }
        }
          level1Menus.sort((entity,entity2)-> {return (entity.getSort()==null?0:entity.getSort())-(entity2.getSort()==null?0:entity2.getSort()); });
        return level1Menus;
    }

    @Override
    public void removeMenuByIds(List<Long> asList) {
        //TODO  检查菜单是否在别的地方引用
        baseMapper.deleteBatchIds(asList);
    }

    @Override
    public Long[] findCategoryPath(Long catelogId) {
        List<Long> list=new ArrayList<>();
        CategoryEntity entity = this.getById(catelogId);
        list.add(catelogId);
        Long parId=entity.getParentCid();
        while (parId!=0){
            list.add(parId);
            CategoryEntity entity1 = this.getById(parId);
            parId=entity1.getParentCid();
        }
        Collections.reverse(list);
        return list.toArray(new Long[list.size()]);
    }

    @Override
    public void updateDetail(CategoryEntity category) {
        this.updateById(category);
        if(!StringUtils.isEmpty(category.getName())){
            categoryBrandRelationService.updateCategory(category.getCatId(),category.getName());
        }
    }

    private List<CategoryEntity> getChildren(CategoryEntity root,List<CategoryEntity> all){

        List<CategoryEntity> children=new ArrayList();

        for (CategoryEntity entity : all) {

            //找到子菜单
            if (entity.getParentCid()==root.getCatId()){

                entity.setChildren(getChildren(entity,all));

                children.add(entity);
            }
    }
        children.sort((entity,entity2)-> {return (entity.getSort()==null?0:entity.getSort())-(entity2.getSort()==null?0:entity2.getSort()); });
            return children;
    }
}